package com.kandinfo.isbnmanage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.kandinfo.base.admin.domain.DictData;
import com.kandinfo.base.admin.enumeration.DictType;
import com.kandinfo.base.admin.service.DictDataService;
import com.kandinfo.base.common.dto.ResultDto;
import com.kandinfo.base.common.dto.ResultDtoFactory;
import com.kandinfo.base.security.SecurityContext;
import com.kandinfo.isbnmanage.domian.Homework;
import com.kandinfo.isbnmanage.domian.HomeworkPage;
import com.kandinfo.isbnmanage.domian.HomeworkTemporary;
import com.kandinfo.isbnmanage.domian.IsbnDistribute;
import com.kandinfo.isbnmanage.domian.IsbnInformation;
import com.kandinfo.isbnmanage.domian.IsbnManage;
import com.kandinfo.isbnmanage.domian.IsbnPage;
import com.kandinfo.isbnmanage.dto.IsbnBusinessReqDto;
import com.kandinfo.isbnmanage.enums.IsbnRoleCodeEnum;
import com.kandinfo.isbnmanage.repository.HomeworkPageQuestionRepository;
import com.kandinfo.isbnmanage.repository.HomeworkPageSubquestionRepository;
import com.kandinfo.isbnmanage.repository.HomeworkTemporaryRepository;
import com.kandinfo.isbnmanage.repository.IsbnBusinessRepository;
import com.kandinfo.isbnmanage.repository.IsbnDistributeRepository;
import com.kandinfo.isbnmanage.repository.IsbnInformationRepository;
import com.kandinfo.isbnmanage.repository.IsbnManageRepository;
import com.kandinfo.isbnmanage.repository.IsbnPageRepository;
import com.kandinfo.isbnmanage.service.HomeworkService;
import com.kandinfo.isbnmanage.service.IsbnAutoDistributeService;
import com.kandinfo.isbnmanage.service.IsbnBusinessService;
import com.kandinfo.isbnmanage.service.IsbnDistributeService;
import com.kandinfo.isbnmanage.service.IsbnInformationService;
import com.kandinfo.isbnmanage.service.IsbnLogService;
import com.kandinfo.isbnmanage.service.IsbnManageService;
import com.kandinfo.isbnmanage.util.PublishUtil;


@Service
public class IsbnBusinessServiceImpl implements IsbnBusinessService {

	@Autowired
	IsbnInformationService isbnInformationService;
	
	@Autowired
	IsbnInformationRepository isbnInformationRepository;
	
	@Autowired
	IsbnDistributeService isbnDistributeService;
	
	@Autowired
	IsbnDistributeRepository isbnDistributeRepository;
	
	@Autowired
	DictDataService dictDataService;
	
	@Autowired
	HomeworkService homeworkService;
	
	@Autowired
	IsbnPageRepository isbnPageRepository;
	
	@Autowired
	IsbnAutoDistributeService isbnAutoDistributeService;
	
	@Autowired
	IsbnManageRepository isbnManageRepository;
	
	@Autowired
	IsbnManageService isbnManageService;
	
	@Autowired
	HomeworkTemporaryRepository homeworkTemporaryRepository;
	
	@Autowired
	IsbnBusinessRepository isbnBusinessRepository;
	
	@Autowired
	HomeworkPageQuestionRepository homeworkPageQuestionRepository;
	
	@Autowired
	HomeworkPageSubquestionRepository homeworkPageSubquestionRepository;
	
	@Autowired
	IsbnLogService isbnLogService;
	
	
	/**
	 * 更新isbn_manage统计状态
	 * 
	 * @param isbnBusinessReqDto
	 */
	private void uploadIsbnManageCount(IsbnBusinessReqDto isbnBusinessReqDto) {
		Integer status = isbnBusinessReqDto.getIsbnInformation().getStatus();
		boolean unpublishFlag = (4 == status && 5 == isbnBusinessReqDto.getCurStatus());
		if (unpublishFlag) {
			//取消发布状态下，同发布一样处理，更新IsbnManage PublishCount
			status = 5;
		}
		if (!(3 == status || 5 == status )) {
			return;
		}
		
		String barcode = isbnBusinessReqDto.getIsbnInformation().getBarcode();
		Integer count = isbnInformationRepository.getCountByBarcodeAndStatus(barcode,status);
		IsbnManage isbnManage = isbnManageRepository.getByBarcode(barcode);
		if (isbnManage == null) {
			isbnBusinessReqDto.setErrMsg("条形码【"+barcode+"】所对应isbnManage信息不存在！");
			return;
		}
		if (3 == status) {
			isbnManage.setUploadCount(count);
		}else if (5 == status) {
			isbnManage.setPublishCount(count);
			isbnManage.setStatus(4);
		}
		isbnManage.setUpdateBy(isbnBusinessReqDto.getCurUserId());
		isbnManage.setUpdateTs(isbnBusinessReqDto.getDate());
		isbnManageService.save(isbnManage);
	}
	
	/**
	 * 审核作业本信息和图片完整性（图片是否处理完成）
	 *  
	 * @param isbnBusinessReqDto
	 */
	private void judgeIsbn(IsbnBusinessReqDto isbnBusinessReqDto) {
		IsbnInformation isbnInformation = isbnBusinessReqDto.getIsbnInformation();
		
		//校验信息完整性
		String msg = checkIsbnInformation(isbnInformation);
		if (StringUtils.isNotEmpty(msg)) {
			isbnBusinessReqDto.setErrMsg(msg);
			return;
		}
		
		//校验页面图片是否上传
		List<HomeworkTemporary> isbnPageList = homeworkTemporaryRepository.findByIsbnInformationId(isbnBusinessReqDto.getIsbnInformationId(),"1");
		if (CollectionUtils.isEmpty(isbnPageList)) {
			isbnBusinessReqDto.setErrMsg("图片信息未上传！");
			return;
		}
		
		//校验图片是否处理完成
		List<HomeworkTemporary> notFinish = homeworkTemporaryRepository.findByIsbnInfoIdAndStatus(isbnBusinessReqDto.getIsbnInformationId(),"0");
		if (!CollectionUtils.isEmpty(notFinish)) {
			isbnBusinessReqDto.setErrMsg("图片信息未处理完成！未处理数量【"+notFinish.size()+"】");
			return;
		}
		
	}

	/**
	 * 校验isbn信息完整性
	 * 
	 * @param isbnInformation
	 * @return
	 */
	private String checkIsbnInformation(IsbnInformation isbnInformation) {
		if (isbnInformation == null) {
			return "isbnInformation信息为空!";
		}
		StringBuffer msg = new StringBuffer();
		if (StringUtils.isEmpty(isbnInformation.getName())) {
			msg.append("作业本名称");
		}
		if (StringUtils.isEmpty(isbnInformation.getBarcode())) {
			if (msg.length() > 0) {
				msg.append("、");
			}
			msg.append("条形码");
		}
		if (isbnInformation.getType() == null) {
			if (msg.length() > 0) {
				msg.append("、");
			}
			msg.append("类型");
		}
		if (isbnInformation.getGrade() == null) {
			if (msg.length() > 0) {
				msg.append("、");
			}
			msg.append("年级");
		}
		if (isbnInformation.getSubject() == null) {
			if (msg.length() > 0) {
				msg.append("、");
			}
			msg.append("科目");
		}
		if (isbnInformation.getTerm() == null) {
			if (msg.length() > 0) {
				msg.append("、");
			}
			msg.append("学期");
		}
		if (isbnInformation.getEdition() == null) {
			if (msg.length() > 0) {
				msg.append("、");
			}
			msg.append("教材版本");
		}
		if (isbnInformation.getHasAnswer() == null) {
			if (msg.length() > 0) {
				msg.append("、");
			}
			msg.append("是否上传答案");
		}
		if (isbnInformation.getValidation() == null) {
			if (msg.length() > 0) {
				msg.append("、");
			}
			msg.append("是否经审定");
		}
		List<HomeworkTemporary> homeworkTemporarieList = homeworkTemporaryRepository.findByIsbnInformationId(isbnInformation.getId(), "0");
		if (CollectionUtils.isEmpty(homeworkTemporarieList)) {
			if (msg.length() > 0) {
				msg.append("、");
			}
			msg.append("封面");
		}else {
			if (StringUtils.isEmpty(isbnInformation.getCoverImageUrl())) {
				msg.append("封面");
			}
		}
		if (msg.length() > 0) {
			msg.append("  信息缺失！");
		}
		return msg.toString();
	}

	/**
	 * 
	 * isbn Information status 业务处理
	 * 
	 * @param isbnBusinessReqDto
	 * @return
	 */
	private IsbnBusinessReqDto dealInformationStatus(IsbnBusinessReqDto isbnBusinessReqDto){
		int curStatus = isbnBusinessReqDto.getCurStatus();
		int nextStatus = isbnBusinessReqDto.getNextStatus();
		
		Long curUserId = isbnBusinessReqDto.getCurUserId();
		Date date = isbnBusinessReqDto.getDate();
		Long isbnInformationId = isbnBusinessReqDto.getIsbnInformationId();
		if (isbnInformationId == null) {
			isbnBusinessReqDto.setErrMsg("isbnInformation Id为空！");
			return isbnBusinessReqDto;
		}
		IsbnInformation isbnInformation = isbnInformationService.findOne(isbnInformationId);
		if (isbnInformation == null) {
			isbnBusinessReqDto.setErrMsg("isbnInformation信息不存在！");
			return isbnBusinessReqDto;
		}
		String barcode = isbnInformation.getBarcode();
		if (StringUtils.isEmpty(barcode)) {
			isbnBusinessReqDto.setErrMsg("isbnInformation信息不完整！条形码为空");
			return isbnBusinessReqDto;
		}
		Integer status = isbnInformation.getStatus();
		Map<String, DictData> informationStatusMap = dictDataService.getValueMapByType(DictType.ISBN_INFORMATION_STATUS);
		if (curStatus != status) {
			isbnBusinessReqDto.setErrMsg("当前isbnInformation状态不是【"+informationStatusMap.get(String.valueOf(curStatus)).getName()+"】！无法进行【"+informationStatusMap.get(String.valueOf(nextStatus)).getName()+"】操作");
			return isbnBusinessReqDto;
		}
		isbnInformation.setStatus(nextStatus);
		isbnInformation.setUpdateTs(date);
		isbnInformation.setUpdateBy(curUserId);
		isbnInformationService.save(isbnInformation);
		isbnBusinessReqDto.setIsbnInformation(isbnInformation);
		isbnBusinessReqDto.setComments(informationStatusMap.get(String.valueOf(nextStatus)).getName());
		return isbnBusinessReqDto;
	}
	
	/**
	 * 
	 * isbn Distribute status 业务处理
	 * 
	 * @param isbnBusinessReqDto
	 * @return
	 */
	private IsbnBusinessReqDto dealDistributeStatus(IsbnBusinessReqDto isbnBusinessReqDto){
		Long curUserId = isbnBusinessReqDto.getCurUserId();
		Date date = isbnBusinessReqDto.getDate();
		
		IsbnInformation isbnInformation = isbnBusinessReqDto.getIsbnInformation();
		String barcode = isbnInformation.getBarcode();
		int type = isbnBusinessReqDto.getType();
		IsbnDistribute isbnDistribute = null;
		if (type == 0) {
			isbnDistribute = isbnDistributeRepository.findByBarcodeAndType(barcode,type);
		}else {
			isbnDistribute = isbnDistributeRepository.findByRefKeyAndType(isbnInformation.getId(),type);
		}
		
		if (isbnDistribute == null) {
			isbnBusinessReqDto.setErrMsg("条形码【"+barcode+"】对应分发任务不存在");
			return isbnBusinessReqDto;
		}
		isbnDistribute.setStatus(1);
		if (0 == type) {
			//购书
			Integer count = isbnDistributeRepository.getInformationFillCandoUndoCount(barcode);
			if (count > 0) {
				//分发任务所带的isbnInformation未  可填充信息填充完成，分发任务设置成不能已完成
				isbnDistribute.setStatus(0);
			}
		}else if (5 == type || 6 == type) {
			//取消发布
			isbnDistribute.setStatus(0);
			isbnDistribute.setUpdateTs(date);
			isbnDistribute.setUpdateBy(curUserId);
			isbnDistributeService.save(isbnDistribute);
		}
		if (1 == isbnDistribute.getStatus()) {
			isbnDistribute.setUpdateTs(date);
			isbnDistribute.setUpdateBy(curUserId);
			isbnDistributeService.save(isbnDistribute);
		}
		return isbnBusinessReqDto;
	}
	
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public ResultDto informationFillCando(IsbnBusinessReqDto isbnBusinessReqDto) throws Exception {
		isbnBusinessReqDto.setDate(new Date());
		isbnBusinessReqDto.setCurUserId(SecurityContext.getInstance().getCurrentUserId());
		
		//处理isbn信息状态
		isbnBusinessReqDto.setCurStatus(0);
		isbnBusinessReqDto.setNextStatus(1);
		dealInformationStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//处理分发记录状态
		isbnBusinessReqDto.setType(0);
		dealDistributeStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//自动分发
		if (!isbnAutoDistributeService.autoDistributeOne(IsbnRoleCodeEnum.ISBN_INFORMATION_COMPLETE, isbnBusinessReqDto.getIsbnInformation().getId())) {
			throw new Exception("自动分发失败");
		}
		
		//添加日志
		isbnLogService.addIsbnLog(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		return ResultDtoFactory.toAck("处理完成!", isbnBusinessReqDto.getIsbnInformation());
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public ResultDto informationFillComplete(IsbnBusinessReqDto isbnBusinessReqDto) throws Exception {
		isbnBusinessReqDto.setDate(new Date());
		isbnBusinessReqDto.setCurUserId(SecurityContext.getInstance().getCurrentUserId());
		
		//处理isbn信息状态
		isbnBusinessReqDto.setCurStatus(1);
		isbnBusinessReqDto.setNextStatus(2);
		dealInformationStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//校验数据完整性
		String msg = checkIsbnInformation(isbnBusinessReqDto.getIsbnInformation());
		if (StringUtils.isNotEmpty(msg)) {
			throw new Exception(msg);
		}
		
		//添加日志
		isbnLogService.addIsbnLog(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		return ResultDtoFactory.toAck("处理完成!", isbnBusinessReqDto.getIsbnInformation());
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public ResultDto pageFillComplete(IsbnBusinessReqDto isbnBusinessReqDto) throws Exception {
		List<HomeworkTemporary> isbnPageList = homeworkTemporaryRepository.findByIsbnInformationId(isbnBusinessReqDto.getIsbnInformationId(),"1");
		if (CollectionUtils.isEmpty(isbnPageList)) {
			throw new Exception("图片信息未上传！");
		}
		
		isbnBusinessReqDto.setDate(new Date());
		isbnBusinessReqDto.setCurUserId(SecurityContext.getInstance().getCurrentUserId());
		
		//处理isbn信息状态
		isbnBusinessReqDto.setCurStatus(2);
		isbnBusinessReqDto.setNextStatus(3);
		dealInformationStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//处理分发记录状态
		isbnBusinessReqDto.setType(1);
		dealDistributeStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//自动分发
		if (!isbnAutoDistributeService.autoDistributeOne(IsbnRoleCodeEnum.ISBN_PUBLISH, isbnBusinessReqDto.getIsbnInformation().getId())) {
			throw new Exception("自动分发失败");
		}
		
		//更新isbn_manage统计状态      图片上传完成的isbn信息数量 upload_count
		uploadIsbnManageCount(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//添加日志
		isbnLogService.addIsbnLog(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		return ResultDtoFactory.toAck("处理完成!", isbnBusinessReqDto.getIsbnInformation());
	}


	@Override
	@Transactional(rollbackFor=Exception.class)
	public ResultDto judgeComplete(IsbnBusinessReqDto isbnBusinessReqDto) throws Exception {
		isbnBusinessReqDto.setDate(new Date());
		isbnBusinessReqDto.setCurUserId(SecurityContext.getInstance().getCurrentUserId());
		
		//处理isbn信息状态
		isbnBusinessReqDto.setCurStatus(3);
		isbnBusinessReqDto.setNextStatus(4);
		dealInformationStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//审核作业本相关信息和图片完整性
		judgeIsbn(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//添加日志
		isbnLogService.addIsbnLog(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		return ResultDtoFactory.toAck("处理完成!", isbnBusinessReqDto.getIsbnInformation());
	}


	@Override
	@Transactional(rollbackFor=Exception.class)
	public ResultDto publish(IsbnBusinessReqDto isbnBusinessReqDto) throws Exception {
		isbnBusinessReqDto.setDate(new Date());
		isbnBusinessReqDto.setCurUserId(SecurityContext.getInstance().getCurrentUserId());
		
		//处理isbn信息状态
		isbnBusinessReqDto.setCurStatus(4);
		isbnBusinessReqDto.setNextStatus(5);
		dealInformationStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//校验数据完整性
		String msg = checkIsbnInformation(isbnBusinessReqDto.getIsbnInformation());
		if (StringUtils.isNotEmpty(msg)) {
			throw new Exception(msg);
		}
		
		//处理分发记录状态
		isbnBusinessReqDto.setType(2);
		dealDistributeStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//更新isbn_manage统计状态      发布完成的isbn信息数量 publish_count
		uploadIsbnManageCount(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		// 更新其他库数据，必须放在最后做
		sysIsbn(isbnBusinessReqDto);
		if (CollectionUtils.isEmpty(isbnBusinessReqDto.getHomeworkPageList())) {
			throw new Exception("作业本不存在已处理图片信息！目前不支持非仅答案作业本，若有疑问请联系开发");
		}
		isbnBusinessRepository.sysIsbn(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//添加日志
		isbnLogService.addIsbnLog(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		return ResultDtoFactory.toAck("处理完成!", isbnBusinessReqDto.getIsbnInformation());
	}
	
	/**
	 * 同步
	 * 
	 * isbn_information -> homework
	 * homework_page    -> homework_page
	 * homework_page_question   ->    homework_page_question
	 * homework_page_subquestion ->	  homework_page_subquestion
	 * @param isbnBusinessReqDto
	 */
	private void sysIsbn(IsbnBusinessReqDto isbnBusinessReqDto) {
		Long informationId = isbnBusinessReqDto.getIsbnInformationId();
		
		Homework homework = synHomework(isbnBusinessReqDto.getIsbnInformation(), "1");
		isbnBusinessReqDto.setHomework(homework);
		
		List<HomeworkPage> homeworkPageList = sysHomeworkPageList(informationId);
		isbnBusinessReqDto.setHomeworkPageList(homeworkPageList);
		
		//todo  homework——others
		
	}
	
	private List<HomeworkPage> sysHomeworkPageList(Long informationId) {
		List<HomeworkPage> homeworkPageList = new ArrayList<HomeworkPage>();
		List<IsbnPage> isbnPageList = isbnPageRepository.findByInformationId(informationId,"1");
		if (CollectionUtils.isEmpty(isbnPageList)) {
			return homeworkPageList; 
		}
		for (IsbnPage isbnPage : isbnPageList) {
			HomeworkPage homeworkPage = sysIsbnPage(isbnPage);
			homeworkPageList.add(homeworkPage);
		}
		return homeworkPageList;
	}

	/**
	 * 同步书页信息
	 * @param isbnPage
	 * @return
	 */
	private HomeworkPage sysIsbnPage(IsbnPage isbnPage) {
		HomeworkPage homeworkPage = new HomeworkPage();
		homeworkPage.setId(isbnPage.getId());
		homeworkPage.setCreateBy(isbnPage.getCreateBy());
		homeworkPage.setCreateTs(isbnPage.getCreateTs());
		homeworkPage.setFileName(isbnPage.getFileName());
		homeworkPage.setHandleProcess(isbnPage.getHandleProcess());
		homeworkPage.setHomeworkId(isbnPage.getIsbnInformationId());
		homeworkPage.setPageImageUrl(isbnPage.getPageImageUrl());
		homeworkPage.setSeqNo(isbnPage.getSeqNo());
		homeworkPage.setStatus(isbnPage.getStatus()+"");
		homeworkPage.setUpdateBy(isbnPage.getUpdateBy());
		homeworkPage.setUpdateTs(isbnPage.getUpdateTs());
		return homeworkPage;
	}

	/**
	 * 同步书本信息
	 * 
	 * @param isbnInformation
	 * @return
	 */
	private Homework synHomework(IsbnInformation isbnInformation,String status) {
		Map<String, DictData> subjectMap = dictDataService.getValueMapByType(DictType.SUBJECT);
		Homework homework = new Homework();
		homework.setId(isbnInformation.getHomeworkId());
		homework.setName(isbnInformation.getName());
		homework.setType(isbnInformation.getType()+"");
		homework.setGrade(isbnInformation.getGrade());
		Long subject = PublishUtil.getPublishSubject(isbnInformation.getSubject(),isbnInformation.getTerm());
		homework.setSubject(subject);
		homework.setSubjectName(subjectMap.get(isbnInformation.getSubject().toString()).getName());
		homework.setTerm(isbnInformation.getTerm()+"");
		homework.setEdition(isbnInformation.getEdition()+"");
		homework.setCoverImageUrl(isbnInformation.getCoverImageUrl());
		homework.setHasAnswer(isbnInformation.getHasAnswer()+"");
		homework.setStatus(status);
		homework.setComments(isbnInformation.getComments());
		homework.setPress(isbnInformation.getPress());
		homework.setTime(isbnInformation.getTime());
		homework.setValidation(isbnInformation.getValidation()+"");
		homework.setCreateTs(isbnInformation.getUpdateTs());
		homework.setCreateBy(isbnInformation.getUpdateBy());
		homework.setBarcode(isbnInformation.getBarcode());
		homework.setRevision(isbnInformation.getRevision());
		return homework;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public ResultDto unpublish(IsbnBusinessReqDto isbnBusinessReqDto) throws Exception {
		isbnBusinessReqDto.setDate(new Date());
		isbnBusinessReqDto.setCurUserId(SecurityContext.getInstance().getCurrentUserId());
		
		//处理isbn信息状态
		isbnBusinessReqDto.setCurStatus(5);
		isbnBusinessReqDto.setNextStatus(4);
		dealInformationStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//处理分发记录状态
		isbnBusinessReqDto.setType(2);
		dealDistributeStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//更新isbn_manage统计状态      发布完成的isbn信息数量 publish_count
		uploadIsbnManageCount(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//同步信息，取消发布
		sysIsbn(isbnBusinessReqDto);
		isbnBusinessRepository.upPulish(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//添加日志
		isbnLogService.addIsbnLog(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		return ResultDtoFactory.toAck("处理完成!", isbnBusinessReqDto.getIsbnInformation());
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public ResultDto drop(IsbnBusinessReqDto isbnBusinessReqDto) throws Exception {
		isbnBusinessReqDto.setDate(new Date());
		isbnBusinessReqDto.setCurUserId(SecurityContext.getInstance().getCurrentUserId());
		
		//处理isbn信息状态
		isbnBusinessReqDto.setCurStatus(3);
		isbnBusinessReqDto.setNextStatus(6);
		dealInformationStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//处理分发记录状态
		isbnBusinessReqDto.setType(2);
		dealDistributeStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		dealDropIsbnManage(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//添加日志
		isbnLogService.addIsbnLog(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		return ResultDtoFactory.toAck("处理完成!", isbnBusinessReqDto.getIsbnInformation());
	}

	private void dealDropIsbnManage(IsbnBusinessReqDto isbnBusinessReqDto) {
		String barcode = isbnBusinessReqDto.getIsbnInformation().getBarcode();
		List<IsbnInformation> unDorpList = isbnInformationRepository.findUnDropListByBarcode(barcode);
		if (CollectionUtils.isEmpty(unDorpList)) {
			IsbnManage isbnManage = isbnManageRepository.getByBarcode(barcode);
			if (isbnManage == null) {
				isbnBusinessReqDto.setErrMsg("条形码【"+barcode+"】所对应isbnManage信息不存在！");
				return;
			}
			isbnManage.setStatus(5);
			isbnManage.setUpdateBy(isbnBusinessReqDto.getCurUserId());
			isbnManage.setUpdateTs(isbnBusinessReqDto.getDate());
			isbnManageService.save(isbnManage);
		}
	}

	@Override
	public ResultDto undrop(IsbnBusinessReqDto isbnBusinessReqDto) throws Exception {
		isbnBusinessReqDto.setDate(new Date());
		isbnBusinessReqDto.setCurUserId(SecurityContext.getInstance().getCurrentUserId());
		//处理isbn信息状态
		isbnBusinessReqDto.setCurStatus(6);
		isbnBusinessReqDto.setNextStatus(3);
		dealInformationStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//处理分发记录状态
		isbnBusinessReqDto.setType(2);
		dealDistributeStatus(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		dealUndropIsbnManage(isbnBusinessReqDto);
		dealDropIsbnManage(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		
		//添加日志
		isbnLogService.addIsbnLog(isbnBusinessReqDto);
		if (StringUtils.isNotEmpty(isbnBusinessReqDto.getErrMsg())) {
			throw new Exception(isbnBusinessReqDto.getErrMsg());
		}
		return ResultDtoFactory.toAck("处理完成!", isbnBusinessReqDto.getIsbnInformation());
	}

	private void dealUndropIsbnManage(IsbnBusinessReqDto isbnBusinessReqDto) {
		String barcode = isbnBusinessReqDto.getIsbnInformation().getBarcode();
		IsbnManage isbnManage = isbnManageRepository.getByBarcode(barcode);
		if (isbnManage == null) {
			isbnBusinessReqDto.setErrMsg("条形码【"+barcode+"】所对应isbnManage信息不存在！");
			return;
		}
		isbnManage.setStatus(3);
		isbnManage.setUpdateBy(isbnBusinessReqDto.getCurUserId());
		isbnManage.setUpdateTs(isbnBusinessReqDto.getDate());
		isbnManageService.save(isbnManage);
	}

	
	
	

}
